package edu.ccut.saturn.workflow.delegate;

import java.sql.Connection;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.Constants;
import edu.ccut.saturn.workflow.common.WorkFlowTools;

public class Reject extends DefaultComponent{
	public void execute(IParameterContext parameterContext) throws Exception {
		String workitemId = (String)parameterContext.getParameterValue(0);
		String assistant = (String)parameterContext.getParameterValue(1);
		String reason = (String)parameterContext.getParameterValue(2);
		//处理协办消息表
		Connection conn = parameterContext.getConnection();
		SaturnData delegateMesgInst = WorkFlowTools.getDelegateMesgInstance();
		delegateMesgInst.set("content", reason);
		delegateMesgInst.set("time", Operation.baseService().GetSystemTime());
		delegateMesgInst.set("message_from", assistant);
		delegateMesgInst.set("workitem_id", workitemId);
		delegateMesgInst.set("operate_type", Constants.WORKITEM_BIZSTATE_REJECT);//拒绝
		Operation.dataBase().insert(delegateMesgInst, conn);
		
		String sql= "select * from wf_delegate where workitem_id="+workitemId+" and state="+Constants.DELEGATE_STATE_RUNNING;	
		SaturnData delegateInst = Operation.dataBase().
				queryBySQL(Constants.SATURNDATA_WF_DELEGATE, sql, conn).get(0);		
		//更新代办表状态
		delegateInst.set("state", Constants.DELEGATE_STATE_INVALID);//无效
		Operation.dataBase().updateByPrimaryKey(delegateInst, "", conn);
		
		//删除所有代办人
		sql = "delete from wf_wiparticipant where workitemid="+workitemId+" and workitemstate="+Constants.WORKITEM_BIZSTATE_HELP;
		Operation.dataBase().deleteBySQL(sql, conn);
		
		//更新委托人权限
		sql = "update wf_wiparticipant set partiintype='Y' where workitemid="+workitemId
		+" and participant='"+delegateInst.get("delegate_from")+"'";
		Operation.dataBase().updateBySQL(sql, conn);
		
		//更新工作项表的业务状态为拒绝
		SaturnData workitemInst = Operation.dataBase()
				.queryByPrimaryKey(Constants.SATURNDATA_WF_WORKITEM_INSTANCE, workitemId, conn);
		workitemInst.set("bizstate", Constants.WORKITEM_BIZSTATE_REJECT);
		Operation.dataBase().updateByPrimaryKey(workitemInst, "", conn);
	}
}
